package com.sicheng.lc.周赛.分类.位运算;

import java.util.HashSet;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/7/4 15:31
 */
public class 统计追加字母可以获得的单词数 {
    //https://leetcode.cn/problems/count-words-obtained-after-adding-a-letter/
    public int wordCount(String[] startWords, String[] targetWords) {
        HashSet<Integer> map = new HashSet<>();
        for (String word : startWords) {
            int x = bit(word);
            map.add(x);
        }

        int res = 0;
        for (String t : targetWords) {
            int y = bit(t);
            for (int i = 0; i < t.length(); i++) {
                if (map.contains(y ^ (1 << (t.charAt(i) - 'a')))) {
                    res++;
                    break;
                }
            }
        }
        return res;
    }

    private int bit(String word) {
        int x = 0;
        for (int i = 0; i < word.length(); i++) {
            x |= 1 << (word.charAt(i) - 'a');
        }
        return x;
    }

    public static void main(String[] args) {
        统计追加字母可以获得的单词数 s = new 统计追加字母可以获得的单词数();
        String[] start = {"g", "vf", "ylpuk", "nyf", "gdj", "j", "fyqzg", "sizec"},
                target = {"r", "am", "jg", "umhjo", "fov", "lujy", "b", "uz", "y"};
        System.out.println(s.wordCount(start, target));
    }
}
